package com.iotproject.educenter.controller.admin;

import com.iotproject.commonutils.R;
import com.iotproject.educenter.entity.Member;
import com.iotproject.educenter.entity.vo.admin.UserQuery;
import com.iotproject.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("admin/educenter/ucenterMember/")
public class AdminUcenterMemberController {

    @Autowired
    UcenterMemberService ucenterMemberService;

    //条件查询带分页的方法
    @PostMapping("pageUserCondition/{current}/{limit}")
    public R pageUserCondition(@PathVariable("current") long current,
                                  @PathVariable("limit") long limit,
                                  @RequestBody(required = false) UserQuery userQuery) {

        Page<Member> ucenterMemberPage = new Page<>(current, limit);
        QueryWrapper<Member> ucenterMemberQueryWrapper = new QueryWrapper<>();

        String userId = userQuery.getId();
        String nickname = userQuery.getNickname();
        String mobile = userQuery.getMobile();
        String sign = userQuery.getSign();
        if (!StringUtils.isEmpty(nickname)) {
            ucenterMemberQueryWrapper.like("nickname", nickname);
        }
        if (!StringUtils.isEmpty(userId)) {
            ucenterMemberQueryWrapper.eq("id", userId);
        }
        if (!StringUtils.isEmpty(mobile)) {
            ucenterMemberQueryWrapper.eq("mobile", mobile);
        }
        if (!StringUtils.isEmpty(sign)) {
            ucenterMemberQueryWrapper.eq("sign", sign);
        }
        ucenterMemberService.page(ucenterMemberPage, ucenterMemberQueryWrapper);
        long total = ucenterMemberPage.getTotal();
        List<Member> records = ucenterMemberPage.getRecords();
        return R.ok().data("total", total).data("rows", records);
    }

    @DeleteMapping("deleteUserById/{id}")
    public R deleteUserById(@PathVariable("id") String id) {
        boolean removeById = ucenterMemberService.removeById(id);
        if (removeById) {
            return R.ok();
        }
        return R.error();
    }

    @GetMapping("getUserById/{id}")
    public R getUserById(@PathVariable("id") String id) {
        Member member = ucenterMemberService.getById(id);
        return R.ok().data("user", member);
    }

    @PostMapping("addUser")
    public R addUser(@RequestBody Member member) {
        boolean save = ucenterMemberService.save(member);
        if (save) {
            return R.ok();
        }
        return R.error();
    }

    @PostMapping("updateUser")
    public R updateUser(@RequestBody Member member) {
        boolean updateById = ucenterMemberService.updateById(member);
        if (updateById) {
            return R.ok();
        }
        return R.error();
    }


}
